package com.epower.web.action.epowerword;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epoweruserword.EpowerUserWordService;
import com.epower.bo.epowerword.EpowerWordService;
import com.epower.dataobject.EpowerSentenceDO;
import com.epower.dataobject.EpowerUserDO;
import com.epower.dataobject.EpowerUserWordDO;
import com.epower.dataobject.EpowerWordDO;
import com.epower.web.action.utils.Constants;
import com.epower.web.action.utils.TemplateUtils;

@Scope("prototype")
@Component()
public class EpowerWordAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private int bookid;
	private int index;
	private int type;
	private EpowerWordService epowerWordBO;
	private EpowerUserWordService epowerUserWordBO;
	private EpowerWordDO epowerWord;
	private Result result = Result.succResult();

	private EpowerWordDO getPreOrNextWord(int bookid,int userid) {//目前获取策略先从db获取一个单词，然后再验证词单词是否是熟词，如果是，则过滤掉重新获取再验证(以后考虑更好的方式)
		EpowerWordDO epowerWord = epowerWordBO.getWordByBookid(bookid, index);// 从db获取单词
		if (epowerWord!=null) {
			EpowerUserWordDO query = new EpowerUserWordDO();
			query.setBookid(epowerWord.getBookid());
			query.setWordid(epowerWord.getId());
			query.setUserid(userid);
			System.out.println("-------->bookid:"+epowerWord.getBookid()+"  wordid:"+epowerWord.getId()+"  userid:"+userid);
			EpowerUserWordDO epowerUserWord = epowerUserWordBO.getEpowerUserWordByOption(query);
			if(epowerUserWord!=null && epowerUserWord.getLevel()==Constants.FAMILLIAR_WORD){// 假如是熟悉词,在继续获下一个词
				if(type == Constants.PRE_WORD){
					--index;
				}else{
					++index;
				}
				return getPreOrNextWord(bookid,userid);
			}
		}
		return epowerWord;
	}

	/**
	 * 获取上一个,下一个单词,index需要前端决定
	 * http://localhost:8080/epower-site-webapp-1.0.0/jsonepowerword/getEpowerWord.action?bookid=6&index=1
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getWordByBookid() throws Exception {
		if (bookid < 0 || index < 0) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			result.setResultCode(Result.RESULTCODE_PARAMETER_ILLEGAL);
			resultMap.put("message", "bookid,index值不能小于0");
			return ERROR;
		}
		EpowerUserDO user = getUser();
		int userid = user.getId();
		epowerWord = getPreOrNextWord(bookid,userid);
		if (epowerWord == null) {
			result.setResultCode(Constants.NO_WORD);
			return SUCCESS;
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		WordTmp word = new WordTmp();
		word.setWordid(epowerWord.getId());
		word.setMeaning(epowerWord.getMeaning());
		word.setSpelling(epowerWord.getSpelling());
		List<EpowerSentenceDO> sentenceList = epowerWord.getSentenceList();// 获取例句
		Map<String, Object> model = new HashMap<String, Object>();
		if(sentenceList.size()>0){
			model.put("sentenceList", sentenceList);
			String tempPath = ServletActionContext.getServletContext().getRealPath("/") + "WEB-INF/freemarker";// 模板路劲
			word.setSentence(TemplateUtils.html2String(model, "sentence.ftl", tempPath));// 加载模板
			resultMap.put("word", word);
			resultMap.put("index", index);
		}
		result.setResultCode(Result.RESULTCODE_NORMAL);
		result.setResult(resultMap);
		return SUCCESS;
	}

	/**
	 * http://localhost:8080/epower-site-webapp-1.0.0/epowerword/viewReadEpowerWord.action
	 * 
	 * @return
	 */
	public String viewReadEpowerWord() throws Exception {
		return SUCCESS;
	}
	
	public int getBookid() {
		return bookid;
	}

	public void setBookid(int bookid) {
		this.bookid = bookid;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public EpowerWordService getEpowerWordBO() {
		return epowerWordBO;
	}

	public void setEpowerWordBO(EpowerWordService epowerWordBO) {
		this.epowerWordBO = epowerWordBO;
	}

	public Result getResult() {
		return result;
	}

	public void setEpowerWord(EpowerWordDO epowerWord) {
		this.epowerWord = epowerWord;
	}

	public EpowerUserWordService getEpowerUserWordBO() {
		return epowerUserWordBO;
	}

	public void setEpowerUserWordBO(EpowerUserWordService epowerUserWordBO) {
		this.epowerUserWordBO = epowerUserWordBO;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public class WordTmp {
		private int wordid;
		private String spelling;
		private String meaning;
		private String sentence;

		public String getSpelling() {
			return spelling;
		}

		public void setSpelling(String spelling) {
			this.spelling = spelling;
		}

		public String getMeaning() {
			return meaning;
		}

		public void setMeaning(String meaning) {
			this.meaning = meaning;
		}

		public String getSentence() {
			return sentence;
		}

		public void setSentence(String sentence) {
			this.sentence = sentence;
		}

		public int getWordid() {
			return wordid;
		}

		public void setWordid(int wordid) {
			this.wordid = wordid;
		}

	}
	// /***查询对象 ***/
	// private EpowerWordDO query;
	//
	// /***对象集合 ***/
	// private List<EpowerWordDO> epowerWordList;
	//
	// /*** BO ***/
	// private EpowerWordService epowerWordBO;
	//
	// /***主键***/
	// private Integer id;
	//
	// /***查询对象 ***/
	// private EpowerWordDO epowerWord;
	//
	// /***当前action所在的namespace ***/
	// private String namespace="epowerword";
	// /***当前action对应的module ***/
	// private String moduleName="EpowerWord";
	//
	// private Result result = Result.succResult();

	// /***
	// * 列表视图
	// * @return
	// * @throws Exception
	// */
	// public String listEpowerWord() throws Exception {
	// //初始化查询条件
	// if (query == null) {
	// query = new EpowerWordDO();
	// }
	// //查询列表
	// epowerWordList = epowerWordBO.getEpowerWordListPageByOption(query);
	// pages = this.getPagesList(query.getToPage(), query.getTotalPage());
	// return SUCCESS;
	// }
	//
	// /***
	// * 编辑视图
	// * @return
	// * @throws Exception
	// */
	// public String viewEditEpowerWord() throws Exception{
	// epowerWord=epowerWordBO.getEpowerWordById(id);
	// return SUCCESS;
	// }
	//
	// /***
	// * 编辑操作
	// * @return
	// * @throws Exception
	// */
	//
	// public String editEpowerWord()throws Exception{
	// if(!epowerWordBO.updateEpowerWordByOption(epowerWord)){
	// addActionMessage("编辑失败");
	// return SUCCESS;
	// }
	// addActionMessage("编辑成功");
	// return SUCCESS;
	// }
	//
	// /***
	// * 删除操作
	// * @return
	// * @throws Exception
	// */
	// public String deleteEpowerWord()throws Exception{
	// if(!epowerWordBO.deleteEpowerWordById(id)){
	// addActionMessage("删除失败");
	// return SUCCESS;
	// }
	// addActionMessage("删除成功");
	// return SUCCESS;
	// }
	// /***
	// * 添加视图
	// * @return
	// * @throws Exception
	// */
	// public String viewAddEpowerWord()throws Exception{
	// return SUCCESS;
	// }
	//
	// /***
	// * 添加操作
	// * @return
	// * @throws Exception
	// */
	// public String addEpowerWord()throws Exception{
	// if(epowerWordBO.addEpowerWord(epowerWord)==null){
	// addActionMessage("添加失败");
	// return SUCCESS;
	// }
	// addActionMessage("添加成功");
	// return SUCCESS;
	// }
	//
	// /*** setter getter start***/
	// public EpowerWordDO getQuery() {
	// return query;
	// }
	// public void setQuery(EpowerWordDO query) {
	// this.query = query;
	// }
	// public List<EpowerWordDO> getEpowerWordList() {
	// return epowerWordList;
	// }
	// public void setEpowerWordList(List<EpowerWordDO> epowerWordList) {
	// this.epowerWordList = epowerWordList;
	// }
	// public EpowerWordService getEpowerWordBO() {
	// return epowerWordBO;
	// }
	// @Resource
	// public void setEpowerWordBO(EpowerWordService epowerWordBO) {
	// this.epowerWordBO = epowerWordBO;
	// }
	// public Integer getId() {
	// return id;
	// }
	// public void setId(Integer id) {
	// this.id = id;
	// }
	// public EpowerWordDO getEpowerWord() {
	// return epowerWord;
	// }
	// public void setEpowerWord(EpowerWordDO epowerWord) {
	// this.epowerWord = epowerWord;
	// }
	// public String getNamespace() {
	// return namespace;
	// }
	//
	// public void setNamespace(String namespace) {
	// this.namespace = namespace;
	// }
	//
	// public String getModuleName() {
	// return moduleName;
	// }
	// public void setModuleName(String moduleName) {
	// this.moduleName = moduleName;
	// }
	//
	// public Result getResult() {
	// return result;
	// }
	//
	// public void setResult(Result result) {
	// this.result = result;
	// }

	/*** setter getter end ***/
}
